package de.wwu.moneyrec;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.LinkedList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.TextView;

public class MoneyRec extends Activity {
	private static final int CAMERA_PIC_REQUEST = 1337; 
	
	public LinkedList<Money> fives = new LinkedList<Money>();
	public LinkedList<Money> tens = new LinkedList<Money>();
	public LinkedList<Money> twentys = new LinkedList<Money>();
	public LinkedList<Money> fiftys = new LinkedList<Money>();
	public LinkedList<Money> one_hundreds = new LinkedList<Money>();
	public LinkedList<Money> two_hundreds = new LinkedList<Money>();
	public LinkedList<Money> five_hundreds = new LinkedList<Money>();
	
	private ImageView imageViewPic;
	public TextView textViewPic;

	private Intent intent;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        imageViewPic = (ImageView) findViewById(R.id.ImageViewPicture);
        textViewPic = (TextView) findViewById(R.id.TextViewPicture);
        
		loadDB();
		
    	// Erstellt ein Intent, welches mit Googles Kamera-Activity verbindet
		intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		startActivityForResult(intent, this.CAMERA_PIC_REQUEST);
    }
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		// hier wird geguckt, ob der requestCode gleich dem CAMERA_PIC_REQUEST ist
		if(requestCode == CAMERA_PIC_REQUEST && data != null){
			
			// die gespeicherte Bitmap wird aus den Daten extrahiert
			Bitmap bitmap = (Bitmap) data.getExtras().get("data");
			
			imageViewPic.setImageBitmap(bitmap);
			
			new ImageOperationDBClusterThread(this, bitmap).run();
		}
	}
    
    private void loadDB(){
    	try {
			InputStream is = this.getAssets().open("imageDBAndroid.imgDB");
			ObjectInputStream inputStream = new ObjectInputStream(is);
			
			LinkedList<LinkedList<Money>> colorDB = new LinkedList<LinkedList<Money>>();
			colorDB = (LinkedList<LinkedList<Money>>) inputStream.readObject();
			
			fives = colorDB.get(0);
			tens = colorDB.get(1);
			twentys = colorDB.get(2);
			fiftys = colorDB.get(3);
			one_hundreds = colorDB.get(4);
			two_hundreds = colorDB.get(5);
			five_hundreds = colorDB.get(6);
			
		} catch (IOException e) {
			Log.e("wichtig", "FEHLER BEIM LEVEL LADEN: " + e.toString());
		} catch (ClassNotFoundException e) {
			Log.e("wichtig", "FEHLER BEIM LEVEL LADEN: " + e.toString());
		}
    }
    
    @Override
	public boolean onTrackballEvent(MotionEvent event) {
		if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
			startActivityForResult(intent, this.CAMERA_PIC_REQUEST);
		}
		
		try{
			Thread.sleep(30);
		}
		catch (Exception e) {
		}
		return true;
	}
}